package com.zzx.controller;

import com.zzx.domain.Manager;
import com.zzx.domain.Vo.AddUserVo;
import com.zzx.domain.Vo.GetUserVo;
import com.zzx.domain.tv.UpdateUser;
import com.zzx.domain.Vo.UserStateVo;
import com.zzx.domain.tv.AddUser;
import com.zzx.domain.tv.LoginTV;
import com.zzx.exception.ExceptionState;
import com.zzx.exception.SystemException;
import com.zzx.service.ManagerService;
import com.zzx.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
public class ManageController {

    @Autowired
    private ManagerService managerService;

    /**
     * 登录
     * @param loginTV
     * @return
     */
    @PostMapping("/user/login")
    public ResponseResult<Manager> Login(@RequestBody LoginTV loginTV){
        Manager manager = new Manager();
        manager.setMgName(loginTV.getUsername());
        manager.setMgPwd(loginTV.getPassword());
        return managerService.login(manager);
    }

    /**
     * 退出
     * @return
     */
    @GetMapping("/user/logout")
    public ResponseResult<Manager> logout(){
        return managerService.logout();
    }


    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('获取用户详情')")
    @GetMapping("/users/{id}")
    public ResponseResult<GetUserVo> getUser(@PathVariable("id")Integer id){
        return managerService.getUser(id);
    }

    /**
     * 查询用户列表
     * @param query
     * @param pagenum
     * @param pagesize
     * @return
     */
    @PreAuthorize("hasAuthority('用户列表')")
    @GetMapping("/users")
    public ResponseResult<Object> getUsers(@RequestParam(required = false) String query, Integer pagenum, Integer pagesize){
            return managerService.getUsers(query,pagenum,pagesize);
    }

    /**
     * 修改用户状态
     * @param uid
     * @param type
     * @return
     */
    @PreAuthorize("hasAuthority('更新用户')")
    @PutMapping("/users/{uid}/state/{type}")
    public ResponseResult<UserStateVo> setState(@PathVariable("uid") Integer uid, @PathVariable("type")Boolean type ){
        System.out.println(uid);
        return managerService.setState(uid,type);
    }

    /**
     * 添加用户
     * @param addUser
     * @return
     */
    @PreAuthorize("hasAuthority('添加用户')")
    @PostMapping("/users")
    public ResponseResult AddUser(@RequestBody AddUser addUser){
        if(addUser.getUsername().trim().length()==0) throw new SystemException( ExceptionState.USERNAMECANNOTBEEMPTY);
        if(addUser.getPassword().trim().length()==0) throw new SystemException(ExceptionState.PASSWORDCANNOTBEEMPTY);
        return managerService.AddUser(addUser);
    }

    /**
     * 删除单个用户
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('删除用户')")
    @DeleteMapping("/users/{id}")
    public ResponseResult DeleteUser(@PathVariable("id") Integer id){
        return managerService.DeleteUser(id);
    }

    /**
     * 通过id更新用户
     * @param id
     * @param updateUser
     * @return
     */
    @PreAuthorize("hasAuthority('更新用户')")
    @PutMapping("/users/{id}")
    public ResponseResult UpdateUser(@PathVariable("id") Integer id,@RequestBody UpdateUser updateUser){
        return managerService.UpdateUser(id,updateUser);
    }

    /**
     * 分配角色
     * @param id
     * @param userVo
     * @return
     */
    @PreAuthorize("hasAuthority('分配用户角色')")
    @PutMapping("/users/{id}/role")
    public ResponseResult setRole(@PathVariable("id") Integer id, @RequestBody AddUserVo userVo){
            return managerService.setRole(id,userVo);
    }

}
